<?php

namespace App\Http\Controllers\Mobile\Personal;

use App\Http\Requests\Personal\Login\AjaxMobileLoginRequest;
use Illuminate\Http\Request;
use App\Models\Personal\PersonalUser;
use Style, Auth;


class MpWeiXinController extends CommonController{

    /**
     * 手机微信绑定模版
     * @param Request $request
     * @return mixed
     */
    public function binDing(Request $request){
        $request->only('unionid', 'openid');
        empty($request->unionid) ? $unionid = '' : $unionid = $request->unionid;
        empty($request->openid) ? $openid = '' : $openid = $request->openid;
        empty($request->category) ? $category = '' : $category = $request->category;
        return Style::view('mobile/personal/auth/binDing', compact('unionid', 'openid','category'));
    }

    /**
     * 保存手机微信绑定
     * @param Request $request
     */
    public function stroyBinDing(Request $request){
        $input = $request->only('unionid', 'openid', 'username', 'password', 'remember', 'category');

        if(!empty($request->openid)){
            $input['unionid'] =  !empty($request->unionid)  ? $request->unionid : null;

            $remember = !empty($input['remember']) ? $input['remember'] : false;
            if(Auth::guard('personal')->attempt(['username' => $input['username'], 'password' => $input['password']], $remember)){
                //普通绑定
                $user = Auth::guard('personal')->user();
                if(!empty($request->category) && $request->category =='qq'){
                    PersonalUser::where('uid', $user->uid)->update(['wechatmpopenid' => $input['openid'], 'wechatunionid' => $input['unionid']]);
                }else{
                    PersonalUser::where('uid', $user->uid)->update(['qqopenid' => $input['openid'], 'qqunionid' => $input['unionid']]);
                }
                $status = "success";
                return Style::view('mobile/personal/auth/binDingStatus', compact('status'));

            }elseif($user = Auth::guard('personal')->attempt(['mobile' => $input['username'], 'password' => $input['password']], $remember)){
                //手机绑定
                $user = Auth::guard('personal')->user();


                if(!empty($request->category) && $request->category =='qq'){
                    PersonalUser::where('uid', $user->uid)->update(['wechatmpopenid' => $input['openid'], 'wechatunionid' => $input['unionid']]);
                }else{
                    PersonalUser::where('uid', $user->uid)->update(['qqopenid' => $input['openid'], 'qqunionid' => $input['unionid']]);
                }
                $status = "success";

                return Style::view('mobile/personal/auth/binDingStatus', compact('status'));

            }else{
                $link = "?unionid=" . $request->unionid . "&openid=" . $request->openid;
                return back()->with(['status' => '账号或密码错误']);
                // return redirect(url("mobile/personal/binding".$link))->with(['status'=>'账号或密码错误']);
                //return Style::view('mobile/personal/auth/binDingStatus',compact('status'));
            }
        }

    }

    /**
     * 保存手机微信绑定-》with手机号码
     * @param Request $request
     */
    public function stroyBinDingWithMobile(AjaxMobileLoginRequest $request){
        $user = PersonalUser::where('mobile', $request->mobile)->first();
        //删除验证码的session数据
        $request->session()->forget('mobile_verification');
        $request->session()->forget('mobile_verification_code');
        if(!empty($user->uid)){
            Auth::guard('personal')->loginUsingId($user->uid);
            $user->wechatmpopenid = $request->openid;
            $user->wechatunionid =  !empty($request->unionid)  ? $request->unionid : null;
            $user->save();
            $status = "success";
        }else{
            $status = "flase";
        }
        return Style::view('mobile/personal/auth/binDingStatus', compact('status'));
    }

    /**
     * 手机微信注册绑定模版
     * @param Request $request
     * @return mixed
     */
    public function regBinDing(Request $request){
        $request->only('unionid', 'openid', 'category');
        Auth::guard('personal')->logout();//清除上次登录
        //以下变量已无效，因为新版中已经用session来保存,所以暂时注册不会自动邦定公众号
        session()->put('unionid', $request->unionid);
        session()->put('openid', $request->openid);
        session()->put('category', $request->category);
        return Style::view('mobile/personal/auth/registerBinding');
    }

}